﻿using Sidvall.Collections;
using System.Linq;

namespace Sidvall.UI.Dialogs
{
    public class MessageOptionCollection : System.Collections.ObjectModel.Collection<MessageOption>
    {
        #region Business Members

        #region DefaultMessageOption

        public MessageOption DefaultMessageOption
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.DefaultMessageOptionId))
                    return null;
                return (from o in this
                        where o.Id.Equals(this.DefaultMessageOptionId, System.StringComparison.OrdinalIgnoreCase)
                        select o).FirstOrDefault();
            }
        }

        #endregion
        #region DefaultMessageOptionId

        private string _DefaultMessageOptionId;
        public string DefaultMessageOptionId
        {
            get
            {
                return _DefaultMessageOptionId;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    _DefaultMessageOptionId = null;
                else
                {
                    var item = (from o in this
                                where o.Id.Equals(value, System.StringComparison.OrdinalIgnoreCase)
                                select o).FirstOrDefault();
                    if (item != null)
                        _DefaultMessageOptionId = value;
                }
            }
        }

        #endregion
        #region CancelMessageOption

        public MessageOption CancelMessageOption
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.CancelMessageOptionId))
                    return null;
                return (from o in this
                        where o.Id.Equals(this.CancelMessageOptionId, System.StringComparison.OrdinalIgnoreCase)
                        select o).FirstOrDefault();
            }
        }

        #endregion
        #region CancelMessageOptionId

        private string _CancelMessageOptionId;
        public string CancelMessageOptionId
        {
            get
            {
                return _CancelMessageOptionId;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    _CancelMessageOptionId = null;
                else
                {
                    var item = (from o in this
                                where o.Id.Equals(value, System.StringComparison.OrdinalIgnoreCase)
                                select o).FirstOrDefault();
                    if (item != null)
                        _CancelMessageOptionId = value;
                }
            }
        }

        #endregion
        #region AnswerIsYes

        public bool AnswerIsYes
        {
            get
            {
                return IsAnswer(Sidvall.Resources.ResourceKeys.YesOptionId);
            }
        }

        #endregion

        #region Add

        public void Add(string id, string message, bool isDefault, bool isCancel)
        {
            var item = new MessageOption(id, message);
            Add(item);
            if (isDefault)
                this.DefaultMessageOptionId = id;
            if (isCancel)
                this.CancelMessageOptionId = id;
        }

        #endregion
        #region AddCancel

        public void AddCancel()
        {
            AddCancel(false, true);
        }
        public void AddCancel(bool isDefault, bool isCancel)
        {
            Add(Sidvall.Resources.ResourceKeys.CancelOptionId, Sidvall.Resources.ResourceKeyManager.Current.CancelOptionId, isDefault, isCancel);
        }

        #endregion
        #region AddNo

        public void AddNo()
        {
            AddNo(false, false);
        }
        public void AddNo(bool isDefault, bool isCancel)
        {
            Add(Sidvall.Resources.ResourceKeys.NoOptionId, Sidvall.Resources.ResourceKeyManager.Current.NoOptionId, isDefault, isCancel);
        }

        #endregion
        #region AddOk

        public void AddOk()
        {
            AddOk(false, false);
        }
        public void AddOk(bool isDefault, bool isCancel)
        {
            Add(Sidvall.Resources.ResourceKeys.OkOptionId, Sidvall.Resources.ResourceKeyManager.Current.OkOptionId, isDefault, isCancel);
        }

        #endregion
        #region AddOkCancel

        public void AddOkCancel(string defaultId)
        {
            Add(Sidvall.Resources.ResourceKeys.OkOptionId, Sidvall.Resources.ResourceKeyManager.Current.OkOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.CancelOptionId, Sidvall.Resources.ResourceKeyManager.Current.CancelOptionId, false, true);
            this.DefaultMessageOptionId = defaultId;
        }

        #endregion
        #region AddYes

        public void AddYes()
        {
            Add(Sidvall.Resources.ResourceKeys.YesOptionId, Sidvall.Resources.ResourceKeyManager.Current.YesOptionId, false, false);
        }

        #endregion
        #region AddYesNo

        public void AddYesNo(string defaultId)
        {
            Add(Sidvall.Resources.ResourceKeys.YesOptionId, Sidvall.Resources.ResourceKeyManager.Current.YesOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.NoOptionId, Sidvall.Resources.ResourceKeyManager.Current.NoOptionId, false, true);
            this.DefaultMessageOptionId = defaultId;
        }

        #endregion
        #region AddYesNoCancel

        public void AddYesNoCancel(string defaultId)
        {
            Add(Sidvall.Resources.ResourceKeys.YesOptionId, Sidvall.Resources.ResourceKeyManager.Current.YesOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.NoOptionId, Sidvall.Resources.ResourceKeyManager.Current.NoOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.CancelOptionId, Sidvall.Resources.ResourceKeyManager.Current.CancelOptionId, false, true);
            this.DefaultMessageOptionId = defaultId;
        }

        #endregion
        #region AddYesNoToAll

        public void AddYesNoToAll(string defaultId)
        {
            Add(Sidvall.Resources.ResourceKeys.YesOptionId, Sidvall.Resources.ResourceKeyManager.Current.YesOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.YesToAllOptionId, Sidvall.Resources.ResourceKeyManager.Current.YesToAllOptionId, false, false);
            Add(Sidvall.Resources.ResourceKeys.NoOptionId, Sidvall.Resources.ResourceKeyManager.Current.NoOptionId, false, true);
            Add(Sidvall.Resources.ResourceKeys.NoToAllOptionId, Sidvall.Resources.ResourceKeyManager.Current.NoToAllOptionId, false, false);
            this.DefaultMessageOptionId = defaultId;
        }

        #endregion
        #region Contains

        public bool Contains(string questionOptionId)
        {
            var item = Find(questionOptionId);
            if (item != null)
                return true;

            return false;
        }

        public bool Contains(string[] questionOptionIds)
        {
            if ((questionOptionIds == null) || (questionOptionIds.Length == 0))
                return false;

            var item = (from o in this
                        where questionOptionIds.Contains(o.Id, System.StringComparer.OrdinalIgnoreCase)
                        select o).FirstOrDefault();
            if (item != null)
                return true;

            return false;
        }

        #endregion
        #region Copy

        public MessageOptionCollection Copy()
        {
            MessageOptionCollection items;

            items = new MessageOptionCollection();
            items.Import(this);
            items.DefaultMessageOptionId = this.DefaultMessageOptionId;
            items.CancelMessageOptionId = this.CancelMessageOptionId;

            return items;
        }

        #endregion
        #region Find

        public MessageOption Find(string questionOptionId)
        {
            if (string.IsNullOrWhiteSpace(questionOptionId))
                return null;

            return (from o in this
                    where o.Id.Equals(questionOptionId, System.StringComparison.OrdinalIgnoreCase)
                    select o).FirstOrDefault();
        }

        #endregion
        #region Import

        public void Import(System.Collections.Generic.IEnumerable<MessageOption> items)
        {
            if (items == null)
                return;

            foreach (var item in items)
            {
                if (!Contains(item))
                    Add(item.Copy());
            }
        }

        #endregion
        #region IsAnswer

        public bool IsAnswer(string answerId)
        {
            if (this.Count != 1)
                return false;
            if (!this[0].Id.Equals(answerId, System.StringComparison.OrdinalIgnoreCase))
                return false;
            return true;
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.JoinToString(", ");
        }

        #endregion

        #endregion
        #region Constructor

        public MessageOptionCollection()
        {
        }

        #endregion
    }
}
